package com.halflife.framework.generator.template.mapper;

import com.halflife.framework.generator.GenerateUtil;
import com.halflife.framework.generator.annotation.QueryAnn;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;

/**
 * Created by zhaoyf on 2018/9/16.
 */
public class CustomField {
    private String name;
    private String typeName;
    private String realName;
    private String customName;
    private String operate;
    private boolean persist = true;
    private boolean refered = false;
    private boolean queryed = false;


    public CustomField(Field field) {
        this.name = field.getName();
        this.realName = name;
        this.operate = "=";
        QueryAnn ann = field.getAnnotation(QueryAnn.class);
        if (ann != null) {
            if (StringUtils.isNotBlank(ann.realValue())) {
                this.realName = ann.realValue();
            }
            if (StringUtils.isNotBlank(ann.custormName())) {
                this.customName = ann.custormName();
            }
            if (StringUtils.isNotBlank(ann.operate())) {
                this.operate = ann.operate();
            }
            this.refered = ann.refered();
            this.queryed = ann.queryed();
            if (!GenerateUtil.isPrimitive(field)) {
                this.persist = false;
            }else
                this.persist = ann.persist();
        }
        try {
            this.typeName = GenerateUtil.getDbType(field.getType().getSimpleName().toLowerCase()).toUpperCase();
        } catch (RuntimeException e) {
            System.out.println(field.getClass().getSimpleName() + ":" + field.getName() + ";" + e.toString());
            ;
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setType(String typeName) {
        this.typeName = typeName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public boolean isPersist() {
        return persist;
    }

    public void setPersist(boolean persist) {
        this.persist = persist;
    }

    public boolean isRefered() {
        return refered;
    }

    public void setRefered(boolean refered) {
        this.refered = refered;
    }

    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public boolean isQueryed() {
        return queryed;
    }

    public void setQueryed(boolean queryed) {
        this.queryed = queryed;
    }

    public String getCustomName() {
        return customName;
    }

    public void setCustomName(String customName) {
        this.customName = customName;
    }
}
